/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package java_io_fundamentals;

import java.io.*;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author arif
 */
public class SampleReadWriteFileStream {

    public static void sampleReadFileStream() {
        System.out.println("=== sample read file by stream");
        try {
            InputStream inputStream = new FileInputStream(new File("res/sample_file.txt"));
            int tempStream = inputStream.read();
            while (tempStream != -1) {
                System.out.print((char) tempStream);
                tempStream = inputStream.read();
            }
        } catch (IOException ex) {
            Logger.getLogger(SampleReadWriteFileStream.class.getName()).log(Level.SEVERE, null, ex);
        }
        System.out.println("");
    }

    public static void sampleWriteFileStream() {
        System.out.println("=== sample write file by stream");
        try {
            try (OutputStream outputStream = new FileOutputStream("res/sample_output.txt")) {
                // char "a" is 97 in ascii
                outputStream.write(97);
            }
        } catch (IOException ex) {
            Logger.getLogger(SampleReadWriteFileStream.class.getName()).log(Level.SEVERE, null, ex);
        }
        System.out.println("");
    }

    public static void main(String args[]) {
        sampleReadFileStream();
        sampleWriteFileStream();
    }
}
